package com.zx._02_thread.基本操作;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class Demo_interrupt {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("start");
//			sleep();
//			waitPlus();
//			park();
//			join();
//			run();
            suspend();
            System.out.println("end");
        });

        t1.start();
        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println(t1.getState());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 唤醒线程继续运行
        t1.interrupt();
    }

    /**
     * @Title: sleep  thread.state = TIMED_WAITING
     * @date: 2020年5月3日下午2:30:17
     * @author: zhangxue
     */
    public static void sleep() {
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            System.out.println("sleep 强制唤醒");
            e.printStackTrace();
        }
    }

    /**
     * @Title: wait  thread.state = WAITING
     * @date: 2020年5月3日下午2:30:17
     * @author: zhangxue
     */
    public synchronized static void waitPlus() {
        try {
            Demo_interrupt.class.wait();
        } catch (InterruptedException e) {
            System.out.println("wait 被强制唤醒");
            e.printStackTrace();
        }
    }

    /**
     * @Title: park  thread.state = WAITING, 不会抛出异常
     *         正常使用 LockSupport.unpark(); 唤醒
     * @date: 2020年5月3日下午2:30:17
     * @author: zhangxue
     */
    public synchronized static void park() {
        try {
            LockSupport.park();
        } catch (Exception e) {
            System.out.println("park 被强制唤醒");
            e.printStackTrace();
        }
    }

    /**
     * @Title: join  thread.state = WAITING, 不会抛出异常
     *         正常使用 LockSupport.unpark(); 唤醒
     * @date: 2020年5月3日下午2:30:17
     * @author: zhangxue
     */
    public synchronized static void join() {
        try {
            Thread.currentThread().join();
        } catch (Exception e) {
            System.out.println("join 被强制唤醒");
            e.printStackTrace();
        }
    }

    /**
     * @Title: run  thread.state = RUNNABLE, 线程正常运行不异常不停止
     * @date: 2020年5月3日下午2:30:17
     * @author: zhangxue
     */
    public static void run() {
        while (true) {
            String s = "";
            for (int i = 0; i < 10000; i++) {
                s += i;
                s.intern();
            }
        }
    }

    /**
     * @Title: suspend  thread.state = RUNNABLE, 线程正常运行不异常不停止
     *         正常使用 resume(); 唤醒
     * @date: 2020年5月3日下午2:30:17
     * @author: zhangxue
     */
    public static void suspend() {
        try {
            Thread.currentThread().checkAccess();
        } catch (Exception e) {
            System.out.println("suspend 被强制唤醒");
            e.printStackTrace();
        }
    }

}
